<?php
##############################################
#
#   规则
# @ Time : 2017-1-3 - 下午10:45:24
# @ Author : JiangYongWei <1085562810@qq.com>
# @ Encoding : UTF-8 language : PHP
#
##############################################

namespace Admin\Controller;
use Admin\Model\RuleprizeModel;
use Admin\Model\PrizeModel;


class RuleprizeController extends AdminBaseController {
	
	private $_rule;
	
	public function _initialize(){
		parent::_initialize();
		$this -> _rule  = new RuleprizeModel();
	}
	
	/**
	 * 列表
	 */
	public function ruleList(){
		$data = $this -> _rule -> getRule(I('rule_name'));
		foreach ($data[0] as $k => $v) {
			$data[0][$k]['a_id'] = $this -> _rule -> getActivityName($v['a_id']);
		}
		
		$this -> assign('rule', $data[0]);
		$this -> assign('page', $data[1]);
		$this -> display('index');
	}


	public function addRule(){
		if(IS_GET){
			$acti = $this -> _rule -> getActivity();
			
			$this -> assign('acti', $acti);
			
			$this -> display('add');
		}else if(IS_POST){
			$data = I('post.');
			$data['statr_time'] = strtotime(I('statr_time'));
			$data['end_time'] = strtotime(I('end_time'));
			$result = $this -> _rule -> addRule($data);
			if($result)
				$this -> success('添加成功', U('ruleList'), 2);
			else
				$this -> error('操作失败', '', 2);
		}
	}

    public function updateRule(){
        if(IS_GET){
            $r_id = I('get.rule_id');
            $data = $this -> _rule -> updateRule($r_id);
            $acti = $this -> _rule -> getActivity();
            $this -> assign('acti', $acti);
            $this -> assign('data', $data);
            $this -> display('update_rule');
        }else if(IS_POST){
            $data = I('post.');
            $data['statr_time'] = strtotime(I('statr_time'));
            $data['end_time'] = strtotime(I('end_time'));
            $result = $this -> _rule -> updateRuleFind($data);
            if($result !== false)
                $this -> success('操作成功', U('ruleList'), 2);
            else
                $this -> error('操作失败', '', 2);
        }

    }

    public function deleteRule(){
        $r_id = I('get.r_id');
        $result = $this -> _rule -> deleteRule($r_id);
        if($result)
            $this -> ajaxReturn(array('status' => 1,'message' => '删除成功'));
        else
            $this -> ajaxReturn(array('status' => 2,'message' => '操作失败'));
    }



	//添加或更新规则奖品
	public function addPrizeAppend(){
		$data = I('post.');
		$result = $this ->  _rule -> addPrizeAppend($data);
		if($result)
			$this -> success('操作成功', U('ruleList'), 2);
		else
			$this -> error('操作失败', '', 2);
	}

	public function editPrizeAppend(){
		$r_id = I('get.rule_id');
		$data = $this -> _rule -> getPrizeAppend($r_id);
		// if($data){
		// 	$this -> assign('act', 'update');
		// }else{
		// 	$this -> assign('act', 'add'); 
		// }
		$prize_xunv = $this -> _rule -> getxunvPrize();
		$prize_shiwu = $this -> _rule -> getshiwuPrize();
		$this -> assign('r_id', $r_id);
		$this -> assign('prize_xunv', $prize_xunv);
		$this -> assign('prize_shiwu', $prize_shiwu);
		$this -> assign('data', $data);
		$this -> display('edit_prize_append');
	}


	
	
}



	
	